package com.violet.themvp.widget;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import com.violet.themvp.R;

/**
 * 通用对话框类
 */
public class LoadingDialog extends AlertDialog {

    private int layoutId;
    private String loadingText;

    public LoadingDialog(@NonNull Context context) {
        this(context, 0);
    }

    public LoadingDialog(@NonNull Context context, @LayoutRes int layout) {
        super(context, R.style.UpdateDialog);
        layoutId = layout;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(layoutId != 0) {
            setContentView(layoutId);
        } else {
            setContentView(R.layout.layout_dialog_loading);
        }
        TextView mTextLoading = findViewById(R.id.text_loading);
        mTextLoading.setText(loadingText);
        //alertDialog是否可以点击外围消失
        setCanceledOnTouchOutside(true);
        setCancelable(true);
        //如果title为空,则隐藏alertDialog标题
        final WindowManager.LayoutParams params = this.getWindow().getAttributes();
        this.getWindow().getDecorView().setPadding(0, 0, 0, 0);
        this.getWindow().getDecorView().setBackgroundResource(0);
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;
        this.getWindow().setAttributes(params);
    }

    public void setLoadingText(String loadingText) {
        this.loadingText = loadingText;
    }
}
